package zbg.cookiePage;



import zbg.uiil.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ACookieServlet", urlPatterns = "/cookieServlet")
public class ACookieServlet extends BaseServlet {

    protected void createCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("k1", "v1");
        Cookie cookie1 = new Cookie("k2", "v2");
        response.addCookie(cookie);
        response.addCookie(cookie1);
        response.getWriter().write("cookie已经被创建");
    }

    protected void getCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies = request.getCookies();
        Cookie iWantCookie = null;
        iWantCookie = CookieUtils.findCookie("k1", cookies);
        if (null != iWantCookie) {
            response.getWriter().write(iWantCookie.getName() + "=" + iWantCookie.getValue() + "<br/>");
        }
    }

    protected void updateCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        方案一
//        Cookie[] cookies = request.getCookies();
//        Cookie updateCookie = new Cookie("k1","updateCookie");
//        response.addCookie(updateCookie);
        Cookie[] cookies = request.getCookies();

        Cookie k1 = CookieUtils.findCookie("k1", cookies);
        k1.setValue("updatecookie");
        response.addCookie(k1);
    }

    protected void cookieDefault(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        cookie.setMaxAge(秒数) 设置cookie的生命时间，正数在多少秒删除，0立即删除，负数浏览器关闭立即删除

        Cookie cookie = new Cookie("defaultck", "defaultck");
        cookie.setMaxAge(-1);
        response.addCookie(cookie);
    }

    protected void cookieNow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        cookie.setMaxAge(秒数) 设置cookie的生命时间，正数在多少秒删除，0立即删除，负数浏览器关闭立即删除

        Cookie cookie = new Cookie("defaultck", "defaultck");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }

    protected void cookieLater(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//      cookie.setMaxAge(秒数) 设置cookie的生命时间，正数在多少秒删除，0立即删除，负数浏览器关闭立即删除

        Cookie cookie = new Cookie("defaultck", "defaultck");
        cookie.setMaxAge(10);
        response.addCookie(cookie);
    }

    protected void cookiePath(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Cookie cookie = new Cookie("path1", "path1");
        //获取工程路径
        String contextPath = request.getContextPath();
        System.out.println(contextPath);
        cookie.setPath(contextPath+"/abc");
        response.addCookie(cookie);
        response.getWriter().write("创建一个带有cookie的path");
    }

}
